package com.sora.order.service.impl;

import com.sora.basic.util.LoginContext;
import com.sora.basic.util.PageList;
import com.sora.order.domain.OrderPetAcquisition;
import com.sora.order.dto.OrderDto;
import com.sora.order.mapper.OrderPetAcquisitionMapper;
import com.sora.order.query.OrderPetAcquisitionQuery;
import com.sora.order.service.IOrderPetAcquisitionService;
import com.sora.basic.service.impl.BaseServiceImpl;
import com.sora.org.domain.Employee;
import com.sora.org.domain.Shop;
import com.sora.org.mapper.EmployeeMapper;
import com.sora.org.mapper.ShopMapper;
import com.sora.pet.domain.Pet;
import com.sora.pet.domain.SearchMasterMsg;
import com.sora.pet.mapper.PetMapper;
import com.sora.pet.mapper.SearchMasterMsgMapper;
import com.sora.user.domain.Logininfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;

/**
 * 业务实现类：
 */
@Service
public class OrderPetAcquisitionServiceImpl extends BaseServiceImpl<OrderPetAcquisition> implements IOrderPetAcquisitionService {

    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private OrderPetAcquisitionMapper orderPetAcquisitionMapper;
    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;
    @Autowired
    private PetMapper petMapper;

    @Override
    public PageList<OrderPetAcquisition> pageQuery(OrderPetAcquisitionQuery query, HttpServletRequest request) {
        Logininfo logininfo = LoginContext.getLogininfo(request);
        Employee employee = employeeMapper.loadByLogininfoId(logininfo.getId());
        if(employee.getShop_id() != null){//代表这是店铺管理员
            Shop shop = shopMapper.loadById(employee.getShop_id());
            if(shop.getAdminId() == employee.getId()){ //代表这是店长
                query.setShopId(employee.getShop_id());
            }
            //代表店员
            query.setEmpId(employee.getId());
        }

        return super.pageQuery(query);
    }

    @Override
    public void cancel(Long id) {
        //将寻主消息放入寻主池，更改状态改为3，将shop_id改为null
        OrderPetAcquisition order = orderPetAcquisitionMapper.loadById(id);
        searchMasterMsgMapper.reject(order.getSearchMasterMsgId());

        //修改订单的状态为-1(取消)
        order.setState(-1);
        orderPetAcquisitionMapper.update(order);
    }

    @Override
    public void confirm(OrderDto orderDto) {
        //订单确认- 确认之后平台就会多一条宠物信息
        Long id = orderDto.getId();
        BigDecimal money = orderDto.getMoney();
        Integer paytype = orderDto.getPaytype();

        //1.宠物录入
        OrderPetAcquisition order = orderPetAcquisitionMapper.loadById(id);
        SearchMasterMsg msg = searchMasterMsgMapper.loadById(order.getSearchMasterMsgId());
        Pet pet = creatPet(money,msg);
        petMapper.save(pet);

        //2.修改订单的状态(1)和成本价
        order.setState(1);
        order.setPrice(money);
        order.setPetId(pet.getId());
        orderPetAcquisitionMapper.update(order);

        //3.修改寻主消息的状态2
        msg.setState(2);
        searchMasterMsgMapper.update(msg);

    }

    private Pet creatPet(BigDecimal money, SearchMasterMsg msg) {
        Pet pet = new Pet();
        pet.setName(msg.getName());
        pet.setCostprice(money);
        pet.setSaleprice(money);
        pet.setTypeId(msg.getPetType());
        pet.setResources(msg.getResources());
        pet.setShopId(msg.getShopId());
        pet.setUserId(msg.getUserId());
        pet.setSearchMasterMsgId(msg.getId());
        return pet;
    }
}
